<template>
  <div class="wrapper">
    <div class="login-card">
      <div class="title"><b>登 录</b></div>
      <el-form :model="user" :rules="rules" ref="userForm">
        <el-form-item label="用户名/姓名" prop="userName" class="form-item">
          <el-input
              size="medium"
              class="input-group"
              prefix-icon="el-icon-user"
              v-model="user.userName">
          </el-input>
        </el-form-item>
        <el-form-item label="密码" prop="userPass" class="form-item">
          <el-input
              size="medium"
              class="input-group"
              prefix-icon="el-icon-lock"
              show-password
              v-model="user.userPass">
          </el-input>
        </el-form-item>
        <el-form-item class="form-item" style="text-align: center">
          <el-button type="primary" @click="handleLogin">登录</el-button>
          <el-button @click="handleCancel">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  name: "UserLoginView",
  data(){
    return {
      user:{
        userName:'',
        userPass:''
      },
      rules:{
        userName:[
          {required:true,message:'用户名/姓名不能为空',trigger:'blur'}
        ],
        userPass:[
          {required:true,message:'密码不能为空',trigger:'blur'}
        ]
      }
    }
  },
  methods:{
    handleLogin(){
      //前端登录
      console.log(this.user);
      this.$refs.userForm.validate(valid => {
        if (valid){
          this.$axios.post("user/login",this.user)
              .then((res)=>{
                if(res.data.code==10001){
                  this.$message.error("用户名或密码错误");
                }
                else if(res.data.code==10005){
                  this.$message.error("用户名或密码为空")
                }
                else {
                  //localStorage
                  window.localStorage.setItem("userToken",res.data.userToken);
                  window.localStorage.setItem("user",JSON.stringify(res.data.user));
                  window.localStorage.setItem("identity1", JSON.stringify(res.data.identity));
                  this.$router.push("/user/manage");
                }
              });
        }
      });
    },
    handleCancel(){
      this.$refs.userForm.resetFields();
    }
  }
}
</script>
<style>
.wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}

.login-card {
  width: 400px;
  padding: 30px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 15px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.login-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 15px 40px rgba(0, 0, 0, 0.15);
}

.title {
  color: #2c3e50;
  font-size: 28px;
  margin-bottom: 24px;
  text-align: center;
  font-weight: 600;
  position: relative;
}

.title::after {
  content: '';
  position: absolute;
  bottom: -10px;
  left: 50%;
  transform: translateX(-50%);
  width: 60px;
  height: 3px;
  background: linear-gradient(90deg, #3498db, #e74c3c);
  border-radius: 2px;
}

.form-item {
  margin-bottom: 24px;
}

.input-group {
  position: relative;
}

.el-input__inner:focus + .prefix-icon {
  color: #3498db;
}
</style>